<?php
@session_start();
ob_start();
/*-----------------------------------------------------------------------------------*/

/* Start WooThemes Functions - Please refrain from editing this section */

/*-----------------------------------------------------------------------------------*/
// WooFramework init

require_once ( get_template_directory() . '/functions/admin-init.php' );	



/*-----------------------------------------------------------------------------------*/

/* Load the theme-specific files, with support for overriding via a child theme.

/*-----------------------------------------------------------------------------------*/



$includes = array(
				'includes/theme-options.php',
				'includes/theme-functions.php',
				'includes/theme-actions.php',
				//'includes/theme-comments.php',
				'includes/theme-js.php',
				'includes/sidebar-init.php',
				'includes/theme-widgets.php',
                'includes/theme-ajax.php',
                'includes/theme-shortcode.php',
);

// Allow child themes/plugins to add widgets to be loaded.

$includes = apply_filters( 'woo_includes', $includes );
foreach ( $includes as $i ) {

	locate_template( $i, true );

}

/**
 * Add function-admin.js library to admin header
 */
 function admin_header_library(){
	$theme_url = get_template_directory_uri();?>
	<!-- Add fancyBox main JS and CSS files -->
    <link type="text/css" rel="stylesheet" href="<?php echo $theme_url;?>/css/admin-theme.css" />
	<script type="text/javascript" src="<?php echo $theme_url ;?>/js/function-admin.js"></script>
	<?php
 }
 add_action("admin_head","admin_header_library");
 
/**
 * Add theme header 
 */
function add_theme_header(){
    $theme_url = get_template_directory_uri();
    ?>
    <!-- Add fancyBox main JS and CSS files -->
    <script type="text/javascript" src="<?php echo get_template_directory_uri();?>/js/fancybox/source/jquery.fancybox.js?v=2.1.4"></script>
    <link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri();?>/js/fancybox/source/jquery.fancybox.css?v=2.1.4" media="screen" />
   <!-- <script src="http://stc.ugc.zdn.vn/link/js/lwb.0.7.js" type="text/javascript"></script>-->
	
    <script src="<?php bloginfo('template_directory'); ?>/js/woothemes-flexslider/jquery.flexslider.js"></script>
	<link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri();?>/js/woothemes-flexslider/flexslider.css" media="screen" />
	
    <!-- Nivo <head> -->
    <link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri();?>/nivo-slider/themes/default/default.css" media="screen" />
	<link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri();?>/nivo-slider/nivo-slider.css" media="screen" />
	<script type="text/javascript" src="<?php echo get_template_directory_uri();?>/nivo-slider/jquery.nivo.slider.js"></script>
    
    <!-- Place in the <head>, after the three links -->
	<?php if(is_home() || is_single()){ ?>
    <script type="text/javascript" charset="utf-8">
	var $ = jQuery;
    $(window).load(function() {
		 
		
        $('.flexslider').flexslider({
        	animation: "slider",
            slideshow: false,
        	itemWidth: 90,
        	minItems: 5,
        	controlNav: false,
        	directionNav: true,
        	itemMargin: 5,
			prevText: "<",
			nextText: ">", 
        });
		 $('.flexslider-single').flexslider({
        	animation: "slider",
            slideshow:true,
        	controlNav: false,
        	directionNav: true,
			
        });
      });
	  

    </script>
    <?php
	}

}
add_action("wp_head","add_theme_header");



/* Show Order table */ 
function edit_order_columns($columns){
	$_columns = array();
	foreach($columns as $key=>$value){
		$_columns[$key] = $value;
		if($key=='title'){
			$_columns['fullname'] = "Khách hàng";
			$_columns['total_price'] = "Giá trị đơn hàng";
		}
	}
    $_columns['title'] = 'Mã đơn hàng';
	$_columns['date'] = 'Ngày đặt';  
    unset($_columns['id']);
	return $_columns;
}
add_filter('manage_edit-orders_columns', 'edit_order_columns' );

function custom_order_status( $column ) {
    global $post;
    switch($column){
        case 'order-status':
            $status_id = get_post_meta($post->ID,'order-status',true);
            $color = get_tax_meta($status_id,"color");
            echo "<span style='background:".$color.";display: block;height:10px;width:190px;'></span>";
            break;
        case 'order-price';
            echo " vnđ";
            break;
	case 'fullname';
            $customer = json_decode(get_post_meta($post->ID,'_customer',true));
			$author = get_user_by("id",$post->post_author);
			echo ($customer&&$customer->fullname)?$customer->fullname:"";
			if($author!=$customer->fullname && $author!="") {
				echo " <span style='color:#FF80C0;'>($author->user_login)</span>";
			}
            break;
		case 'total_price':
			$order_detail = json_decode(get_post_meta($post->ID,'order-detail',true));
			if($order_detail){ echo number_format($order_detail->order_price,0,',','.').' vnđ'; }
			break;
    }
    return $column;
}
add_action('manage_orders_posts_custom_column', 'custom_order_status');

/**
 * Add more filter order allow custumer
 */
function order_filter_custumer( $query ) {
  global $pagenow, $typenow;

  if ( 'orders' == $pagenow ) {
	$filters = get_users(array('fields'=>'all'));
	foreach($filters as $user){
		$var = &$query->query_vars['author_name'];
		if ( isset( $var ) ) {
			$var = $user->user_login;
		}
	}
  }
}
add_filter( 'parse_query', 'order_filter_custumer' );

function order_filter_restrict_manage_posts() {
    global $typenow;

    $post_types = get_post_types( array( '_builtin' => false ) );

	
	if ( in_array( $typenow, $post_types ) && $typenow=='orders') {
		$filters = get_users(array('fields'=>'all'));
		?>
		<select id="author_name" name="author_name" class="postform">
			<option value="">Show all Customer...</option>
			<?php foreach ( $filters as $user ) { ?>
			<option<?php echo (get_query_var("author_name")==$user->user_login)?" selected='selected'":""?> value="<?php echo $user->user_login;?>"> <?php echo $user->user_login; ?></option>			
			<?php } ?>
		</select>
		<?php 
	}
}
add_action( 'restrict_manage_posts', 'order_filter_restrict_manage_posts' );


/**
 * Add maps popup to footer
 */
 function add_map_footer(){
	if(!is_page("lien-he")) {
	?>
	<div id="box-showmap" style="display:none;">
		<?php get_template_part("templates/general","maps");?>
	</div>
	<?php
	}
 }
add_action("woo_foot","add_map_footer");

/*
Plugin Name: Get Rid of Comment Websites
*/
function my_custom_comment_fields( $fields ){
  if(isset($fields['url']))
    unset($fields['url']);
  return $fields;
}

add_filter( 'comment_form_default_fields', 'my_custom_comment_fields' );

/**
 * Remve title in wp_list_categories
 */
function wp_list_categories_remove_title_attributes($output) {
    $output = preg_replace('` title="(.+)"`', '', $output);
    return $output;
}
add_filter('wp_list_categories', 'wp_list_categories_remove_title_attributes');



/**
 * Add meta
 */
 function product_meta_share(){ 
    global $post;
   if($post && $post->post_type=='san-pham'){ ?>
    <meta name="description" content="<?php echo strip_tags(get_the_excerpt());?>" />
    <?php } 
 }
 add_action("woo_meta","product_meta_share");
 
 
/**
* Remove metabox muc gia
*/  
/*function remove_price_range_metabox(){
	remove_meta_box('muc-giadiv', 'san-pham', 'normal');
}	
add_action("admin_menu","remove_price_range_metabox");*/


/**
 * Update taxonomy muc gia
 */  
function update_post_price_range($post_id){
	$slug = 'san-pham';
    /* check whether anything should be done */
	$post = get_post($post_id);
	
    if ( $post->post_type != $slug ) {
        return;
    }
	
	if ( !current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
	
	$price = get_post_meta($post_id,"gia-ban",true);
	$price_sale = get_post_meta($post_id,"gia-khuyen-mai",true);
	
	if($price_sale!="" && $price_sale<$price){
		$price = $price_sale;
	}
	
	$tax = "muc-gia";
	$terms = get_terms($tax ,array('hide_empty'=>false));
	$list_term = array();
	foreach($terms as $term){
		$min = get_tax_meta($term,'min');
		$max = get_tax_meta($term,'max');
		if($min<=$price && $price<=$max){
			$list_term[] = $term->slug;
		}
	}
	wp_set_object_terms($post_id, $list_term ,$tax);
}
//add_action("save_post","update_post_price_range");


/**
 * Update taxonomy muc gia
 */  
function update_quantity_inStock($post_id){

	$slug = 'orders';
    /* check whether anything should be done */
	$post = get_post($post_id);
	
    if ( $post->post_type != $slug ) {
        return;
    }
	
	if ( !current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
	$data_orders=json_decode(get_post_meta($post->ID,"order-detail",true),true);
	
	$status=$_POST['order-status'];
	if(get_post_meta($post_id,"complete-status",true)==1) return;
	
	if($status=="132") //// ID cua tinh trang hoan thanh  , neu no la hoan thanh
	{
		$order_list_item = (array)$data_orders['checkout'];
		foreach ($order_list_item as $datas)
		{
			list($data_product)=json_decode(get_post_meta($datas['idproduct'],"size-product",true),true); 
			$id_child=$datas['size'];
			$id_parent=get_term_top($id_child,"size");
			$data_product[$id_parent][$id_child] -= (int)$datas['qty'];
			update_post_meta($datas['idproduct'],"size-product",json_encode($data_product));
		}
		//update_post_meta($post->ID,"test_bug",json_encode(array($prev_status,get_post_meta($post_id,"order-status",true))));
		//update_post_meta($post->ID,"test_bug",json_encode($data_product));
		update_post_meta($post->ID,"complete-status",1);	
	}
	return false;
}
	
//add_action("save_post","update_quantity_inStock");


function get_term_top($term_id,$tax)
{
	$term = get_term($term_id,$tax);
	if($term->parent==0)
	{
		return $term->term_id;
	}
	$term = get_term($term->parent,$tax);
	return $term->term_id;
}
/**
 * Create & Update term price range
 */
function update_term_price_range( $term_id, $tt_id ){
	$taxonomy = 'muc-gia';	
	
	$min = isset($_POST['min'])?$_POST['min']:get_tax_meta($term_id,'min');
	$max = isset($_POST['max'])?$_POST['max']:get_tax_meta($term_id,'max');
	
	if($min!="" && $max != ""){ 
		global $post;
		$the_query = new WP_Query(array(
			'post_type' => 'san-pham',
			'posts_per_page' => -1
		));
		
		while($the_query->have_posts()){ $the_query->the_post();
			$price = get_post_meta($post->ID,"gia-ban",true);
			$price_sale = get_post_meta($post->ID,"gia-khuyen-mai",true);
			if($price_sale!="" && $price_sale<$price){
				$price = $price_sale;
			}
			if($min<=$price && $price<=$max){
				wp_set_object_terms($post->ID, array($term_id) ,$taxonomy);
			}
		}
		wp_reset_query(); 
	}
}
add_action( 'edit_muc-gia', 'update_term_price_range', 10, 3 );
add_action( 'create_muc-gia', 'update_term_price_range', 11, 3 );


/**
 * Show top banner
 */
 function show_top_banner(){
    global $post;
    $the_query = new WP_Query(array(
        'post_type' => 'banner',
        'status' => 'publish',
        'orderby' => 'meta_value ID',
        'meta_key' => 'thu-tu',
        'order' => 'asc',
        'tax_query' => array(
            array(
                'taxonomy' => 'loai-banner',
                'field' => 'slug',
                'terms' => 'banner-trang-chu-tren'
            )
        )
    ));
    if($the_query->have_posts()){ ?>
    <ul>
        <?php while($the_query->have_posts()){ $the_query->the_post(); ?>
        <li><a href="<?php echo get_post_meta($post->ID,"link",true);?>"><?php the_post_thumbnail("thumbnail");?></a></li>
        <?php }  wp_reset_query(); ?>
    </ul>
    <?php } 
}

/**
 * Get price product format
 */
function get_format_price_product($price){
    if($price!=""){
        return number_format(intval($price),0,".",",") ." VND";
    }else{
        return "Liên hệ"; 
    }
}

/**
 * Show price
 */
function show_price_product($price){
    echo get_format_price_product($price);
}


/**
 * get id first list images
 */
function get_id_first($images)
{
	$axe=array();
	foreach ($images as $API){
		 $axe[1]=$API['id'];
		break;
	 }
	 return $axe; 
}
 
/**
 * Cat chuoi
 */
function cut_string($string, $max_length) {
    if($string && $max_length){
        if (strlen($string) > $max_length){
             $string = substr($string, 0, $max_length);
             $pos = strrpos($string, " ");
          if($pos === false) {
                   return substr($string, 0, $max_length)." . . .";
                }
             return substr($string, 0, $pos)." . . .";
         }else{
             return $string;
         }
    }
}

/**
 * Rewrite pagination
 */ 
function tax_rewrite_rules( $rewrite_rules ) {
    $new_rules = array( 
        'danh-muc/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?danh-muc=$matches[1]&page=$matches[2]',
        'muc-gia/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?muc-gia=$matches[1]&page=$matches[2]',
        'color/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?color=$matches[1]&page=$matches[2]',
        'size/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?size=$matches[1]&page=$matches[2]',
     );
    $rewrite_rules = $new_rules + $rewrite_rules;
    return $rewrite_rules;
}
add_filter('rewrite_rules_array', 'tax_rewrite_rules');

/**
 * Rewrite pagination
 */ 
function get_one_price($id)
{
	 $data=get_post($id);
	 $price_normal=get_post_meta($id,"price-tmp",true);
	 $price_sale=get_post_meta($id,"sale-price-tmp",true);
	 if($price_sale=="")
		{
			$price=$price_price_normal;
		}
	 else
		{
			$price=$price_sale;
		}
	return array("price_one"=>$price,"price_normal"=>$price_normal,"price_sale"=>$price_sale);
}

function show_format_price($id,$price)
{
	$data=get_one_price($id);	
	$price=number_format($data[$price],0,",",".")." VNĐ";
	return $price;
}

function get_Api($id)
{
	get_post($id);
}

function jamnews_addcate_banner()
{
	register_taxonomy_for_object_type("danh-muc","banner"); 
}
add_action("init","jamnews_addcate_banner");

function get_parent_size($data)
{
   foreach ($data as $option => $parent_size) {
						foreach ($parent_size as $k1=>$v1)
					{
							$term_size_parent=get_term($k1,"size");				
								echo "<option value='{$term_size_parent->term_id}'>{$term_size_parent->name}</option>";
					}
	  } 
}




/* Admin coupons */
 function check_coupons(){

	global $post;
	$array=array();
	/// thoi gian cua ngay hien tai
	$_current_date = strtotime(date("Y-m-d"));
	
	// Kiem tra coupon nao het han
	$posts = get_posts("post_type=coupons");
	
	
	foreach($posts as $_post) {
		$start_date = get_post_meta($_post->ID,"form_date",true);
		$end_date = get_post_meta($_post->ID,"to_date",true);
		$coupon_number = intval(get_post_meta($_post->ID,"coupon_number1",true));
	    
		/*$array=array("start_date"=>$start_date,"end_date"=>$end_date,"coupon_number"=>$coupon_number);
		return $array;
		exit();	*/
		
		
		if($start_date==""){
			$start_date = date("Y-m-d");
		}
		if($end_date==""){
			$end_date = date("Y-m-d");
		}
		
		
		$_start_date = strtotime($start_date);
		$_end_date = strtotime($end_date);
		
		$array=array("start_date"=>$start_date,"end_date"=>$end_date,"coupon_number"=>$coupon_number);
		
		
		if( $_current_date<$_start_date || $_end_date<$_current_date || $coupon_number== 0 ){
			wp_update_post(array(
				'ID' => $_post->ID,
				'post_status' => 'pending'
			));
		}else{
			wp_update_post(array(
				'ID' => $_post->ID,
				'post_status' => 'publish'
			));
		}
	}
	
	
	// Kiem tra sinh nhat gui coupon giam gia chuc mung
	$users = get_users("role=subscriber");
	foreach($users as $user)
	{
		$birthday = trim(get_user_meta($user->ID,"birthday", true));
		if($birthday!="")
		{
			list($day,$month,$year) = explode("-",$birthday);
			list($_day,$_month,$_year) = explode("-",date( "d-m-Y" ,strtotime ( '+1 day' , $_current_date  ) ) );
			// Neu ngay mai la sinh nhat cau nguoi nay thi gui mail
			if($day==$_day && $month==$_month)
			{
				send_mail_happy_birthday_to($user);
			}
			
		}
	}
	
 }
 add_action("init","check_coupons");

 /**
  * Gui mail chuc mung sinh nhat cho thang
  */
function send_mail_happy_birthday_to($user)
{
	$arg = array(
		'post_type' => 'coupons',
		'author' =>$user->ID,
		'posts_per_page' => 1,
		'post_status' => 'publish',
		'tax_query' => array(
			array(
				'taxonomy' => 'phan-loai',
				'field' => 'slug',
				'terms' =>  'happy-birthday'
			)
		),
		'date_query' => array(
				array(
				'year' => date("Y")
			)
		)
	);
	list($newpost) = get_posts($arg);

	if(!$newpost)
	{
		
		/*$post_id = wp_insert_post(array(
		    'post_title'=> 'Khuyến mãi sinh nhật của tài khoản '.$user->display_name,		
			'post_type' => 'coupons',
			'author' => $user->ID,
			'post_status' => 'publish',
		));*/
		$brg=array(
		'post_title'=>'Khuyến mãi sinh nhật của tài khoản '.$user->display_name,
		'post_status'=>'publish',
		'post_type'=>'coupons',
		'post_author'=>$user->ID
		);
		$post_id=wp_insert_post($brg);
		wp_set_object_terms($post_id, array('happy-birthday'),'phan-loai');
		global $code;
		$coupon_code = strtoupper( substr( md5(mktime()), 0, 12 )  );
		$code=$coupon_code;
		update_post_meta($post_id,"code_coupon", $coupon_code );
		update_post_meta($post_id,"coupon_number1", 1 );
		update_post_meta($post_id,"form_date", date("Y-m-d") );
		
		$_current_date = strtotime(date("Y-m-d"));
		$next_year = date( "Y-m-d" ,strtotime ( '+1 year' , $_current_date  ) );
		update_post_meta($post_id,"to_date", $next_year );
		update_post_meta($post_id,"coupon_type","on");
		update_post_meta($post_id,"coupon_value",10);
		
		// Gui mail
		ob_start();
		get_template_part("content","email-birthday");
		$MAIL=ob_get_clean();
		wp_mail("hnthuc1990@gmail.com","Mail nhận mã coupon sinh nhật",$MAIL);   

		
	}
}
/* ADD FIELD USER */
/**
 * Add user meta field
 */
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>

	<h3>Thông tin chung khách hàng</h3>
	
    <table class="form-table">
           <tr>  
            <th><label for="phone">Birthday:</label></th>
			<td>
				<input type="text" name="birthday" id="birthday" value="<?php echo esc_attr( get_the_author_meta( 'birthday', $user->ID ) ); ?>" class="regular-text" /><br />
			</td>
		  </tr>
	</table>
    <table class="form-table">
           <tr>  
            <th><label for="phone">Giới tính:</label></th>
			<td>
				<input type="text" name="gioi_tinh" id="gioi_tinh" value="<?php echo esc_attr( get_the_author_meta( 'gioi_tinh', $user->ID ) ); ?>" class="regular-text" /><br />
			</td>
		  </tr>
	</table>
	<table class="form-table">
           <tr>  
            <th><label for="phone">Phone:</label></th>
			<td>
				<input type="text" name="phone" id="phone" value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" /><br />
			</td>
		  </tr>
	</table>
    <table class="form-table">
           <tr>  
            <th><label for="phone">Địa chỉ:</label></th>
			<td>
				<input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br />
			</td>
		  </tr>
	</table>
<?php }

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {
	if ( !current_user_can( 'edit_user', $user_id ) )
		return false;
		update_usermeta($new_user, 'gioi_tinh', $_POST['slGender']==0?"Nam":"Nữ" );
		update_usermeta($new_user, 'phone', $_POST['txtPhone'] );
		update_usermeta($new_user, 'birthday', $_POST['txtBirthday'] );
		update_usermeta($new_user, 'address', $_POST['txtAddress'] );
}
/* ADD FIELD USER */



/* comment product*/
function add_comment_product()
{
	$responce = array(); 
	$request = $_REQUEST;
	foreach($request as $key => $value)
	{
		if(trim($value)=="")
		{
			return false;
		}
	}
	
	// insert comment
	if(is_user_logged_in)
	{
		global $current_user;
		$current_user = wp_get_current_user();
		$data = array(
			'comment_post_ID' => $request['id_post'],
			'comment_author' => $request['comment_name'],
			'comment_content' => $request['comment_content'],
			'comment_author_email' => $current_user->user_email,
			'user_id' => $current_user->ID,
			'comment_approved' => 0,
		);
	}
	else
	{
		$data = array(
			'comment_post_ID' => $request['id_post'],
			'comment_author' => $request['comment_name'],
			'comment_content' => $request['comment_content'],
			'comment_approved' => 0,
		);
	}
	$comment_id = wp_insert_comment($data);
	update_comment_meta( $comment_id, 'comment_title', $request['comment_title'] );
	update_comment_meta( $comment_id, 'comment_rating_chatluong', $request['chatluong'] );
	update_comment_meta( $comment_id, 'comment_rating_hinhdang', $request['hinhdang'] );
	update_comment_meta( $comment_id, 'comment_rating_gia', $request['gia'] );
	
	// update trung binh comment_post
	$start_argv = ceil(( (int)$request['chatluong'] + (int)$request['hinhdang'] + (int)$request['gia'] )/3 );
	if($start_argv>0)
	{
		$current_number = (int)get_post_meta($request['id_post'], "comment_argv_".$start_argv, true ) + 1;
		update_post_meta( $request['id_post'], "comment_argv_".$start_argv, $current_number);
	}
	$star_1=get_post_meta($request['id_post'],'comment_argv_1',true);
    $star_2=get_post_meta($request['id_post'],'comment_argv_2',true);
    $star_3=get_post_meta($request['id_post'],'comment_argv_3',true);
    $star_4=get_post_meta($request['id_post'],'comment_argv_4',true);
    $star_5=get_post_meta($request['id_post'],'comment_argv_5',true);
    if($star_1==""){$star_1=0; } 
    if($star_2==""){$star_2=0; }
    if($star_3=="") {$star_3=0;} 
    if($star_4==""){$star_4=0;} 
    if($star_5==""){$star_5=0;} 
    $star_argv=ceil(($star_1*1+$star_2*2+$star_3*3+$star_4*4+$star_5*5)/($star_1+$star_2+$star_3+$star_4+$star_5));
    $tong=$star_1+$star_2+$star_3+$star_4+$star_5;

    wp_set_object_terms($request['id_post'],"rating-".$star_argv,'rating');
	return $comment_id;
}

function theme_process_add_comment(){
	if(isset($_REQUEST['action']) && $_REQUEST['action']=="add_comment_product")
	{
		$id_comment = add_comment_product();
		if($id_comment)
		{
			wp_redirect( get_permalink()."#comment-".$id_comment );
			die();
		}
	}
}
add_action("wp_head", "theme_process_add_comment");


function product_comment_format_html($comment, $args, $depth)
{
	$GLOBALS['comment'] = $comment;
		extract($args, EXTR_SKIP);

		if ( 'div' == $args['style'] ) {
			$tag = 'div';
			$add_below = 'comment';
		} else {
			$tag = '<li';
			$add_below = 'div-comment';
		}
?>
		<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
		<?php if ( 'div' != $args['style'] ) : ?>
		<div id="div-comment-<?php comment_ID() ?>" class="comment-body">
		<?php endif; ?>
		<div class="comment-author vcard pull-left">
		<?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
		<?php printf(__('<cite class="fn">%s</cite> <span class="says"></span>'), get_comment_author_link()) ?>
		</div>
<?php if ($comment->comment_approved == '0') : ?>
		<em class="comment-awaiting-moderation"> &nbsp;(<?php _e('Your comment is awaiting moderation.') ?>)&nbsp; </em>
		<br />
<?php endif; ?>

		<div class="comment-meta commentmetadata pull-right"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
			<?php
				/* translators: 1: date, 2: time */
				printf( __('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','' );
			?>
		</div>
		<div class="clearfix"></div>
		
		<div class="row-fluid">
			 
			<div class="span4">
				<div class="row-fluid">
					<div class="span4">
						Hình dạng
					</div>
					<div class="span8">
						<p class="stars">
							<span id="chatluongs" data_input="chatluong">
								<?php $comment_rating_chatluong = (int)get_comment_meta( $comment->comment_ID, "comment_rating_chatluong", true); ?>
								<?php for($i=1;$i<=5;$i++){?>
								<a class="star-<?php echo $i;?> <?php echo ($comment_rating_chatluong==$i)?"active":"";?>" rel="" href="#"><?php echo $i;?></a>
								<?php } ?>
							</span>
						 </p>
					 </div>
				 </div>
			 </div>
			 
			<div class="span4">
				<div class="row-fluid">
					<div class="span4">
						Chất lượng
					</div>
					<div class="span8">
						<p class="stars">
							<span id="chatluongs" data_input="chatluong">
								<?php $comment_rating_hinhdang = (int)get_comment_meta( $comment->comment_ID, "comment_rating_hinhdang", true); ?>
								<?php for($i=1;$i<=5;$i++){?>
								<a class="star-<?php echo $i;?> <?php echo ($comment_rating_hinhdang==$i)?"active":"";?>" rel="" href="#"><?php echo $i;?></a>
								<?php } ?>
							</span>
						 </p>
					 </div>
				 </div>
			 </div>
			 
			 <div class="span4">
				<div class="row-fluid">
					<div class="span4">
						Giá
					</div>
					<div class="span8">
						<p class="stars">
							<span id="chatluongs" data_input="chatluong">
								<?php $comment_rating_gia = (int)get_comment_meta( $comment->comment_ID, "comment_rating_gia", true); ?>
								<?php for($i=1;$i<=5;$i++){?>
								<a class="star-<?php echo $i;?> <?php echo ($comment_rating_gia==$i)?"active":"";?>" rel="" href="#"><?php echo $i;?></a>
								<?php } ?>
							</span>
						 </p>
					 </div>
				 </div>
			 </div>
		 </div>
		<div class="clearfix"></div>
		
		<h5 class="comment-title"><?php echo get_comment_meta( $comment->comment_ID, "comment_title", true);?></h5>
		<?php comment_text() ?>

		<div class="reply">
		<?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
		</div>
		<?php if ( 'div' != $args['style'] ) : ?>
		</div>
		<?php endif;
}


function get_iduser($data)
{
	return $data;
}

function SelectionSort($a, $n)  
{  
     $vtmin="";
	 $i="";
	 $j="";
	 $tg="";  
     for($i=$n-1;$i>0;$i--)  
     {  
      $vtmin=$i;  
      for($j=$i+1; $j<$n; $j++)  
        if ($a[$j]<$a[$vtmin]) $vtmin=$j;  
      if($vtmin!=$i) //Hoán đổi phần tử a[vtmin] với a[i]  
      {  
         $tg=$a[$vtmin];    $a[$vtmin]=$a[$i];     $a[$i]=$tg;  
      }  
     } 
	 
	 return $a;
} 



/**
 * @author: phongnd 
 * get_term_recursive
 */
function get_price($post)
{
	$strPrice = get_post_meta($post->ID,"cost-deal",true)." $$$";
	
	return $strPrice;
}


/**
 * @author: phongnd 
 * get_term_recursive
 */
function get_term_recursive( $strSlug_Term, $strSlug_Taxonomy)
{
	$the_term = get_term_by( 'slug',  $strSlug_Term, $strSlug_Taxonomy );
	
	//get list of terms: $childs = get_terms("categories","parent=$the_term->term_id"); //&hide_empty=0
	
	$terms_parent = get_term_by("id", $the_term->parent, $strSlug_Taxonomy);
	// var_dump($strSlug_Taxonomy);
	// var_dump($the_term);
	// var_dump($terms_parent);
	$strRe = "";
	$strLink = "";
	$strTermURL = get_term_link($strSlug_Term, $strSlug_Taxonomy);
	$strLink = $the_term != null ? "<a href='". $strTermURL . "'>". $the_term->name."</a>" : "";
	if($terms_parent != NULL)
	{
		
		//$strRe = $the_term != null ? get_term_recursive( $terms_parent->slug, $strSlug_Taxonomy) . " '>1' " . $strLink : "";
		$strRe = $the_term != null ? get_term_recursive( $terms_parent->slug, $strSlug_Taxonomy) . " 1> " . $strLink : "";
		
	}
	else
	{
		$strRe = $the_term != null ? " 2> " . $strLink : "";
	
	}
	
	return $strRe;
}

/**
 * @author: phongnd 
 * get_link_to_last_term
 */
function get_link_to_last_term( $strSlug_Term, $strSlug_Taxonomy)
{
	//
	// $the_tax->url... is equal: get_term_link($strSlug_Term, $strSlug_Taxonomy);
	$the_tax = get_taxonomy($strSlug_Taxonomy); 	//test: var_dump($the_tax);
	$strReturn =  "<a href='". $the_tax->url. "'>". $the_tax->labels->name."</a>" . get_term_recursive( $strSlug_Term, $strSlug_Taxonomy);
	
	
	return $strReturn;
    // list($child) = get_terms($parent->taxonomy,"parent={$parent->term_id}");
    // if(!$child)
    // {
        // return $parent->name;
    // }
    // $parent_term_link = get_term_link($parent,"categories");
    // $child_term_link = get_term_link($child,"categories");
    
    // $child_name = recursively_readcrumb($child);
    // $child_name  = ($child_name!="")?"/<a href='{$child_term_link}'>".$child_name."</a>":"";
    // return "<a href='{$parent_term_link}'>".$parent->name."</a>".$child_name;
    
} 


/**
 * show_custom_menu($strMenuName), show_main_menu_custom()
 * @author: PhongND - aaa.....
 * @param none.
 * @return none.
 */
function show_custom_menu($strMenuName){
    $main_menus = wp_get_nav_menu_items($strMenuName); ?>
    <ul id="main-nav" class="nav nav-active"> <?php
	if($main_menus != null)
	{
		foreach($main_menus as $menu){
			?>
			<li id="menu-item-<?php echo $menu->ID;?>" class="menu-item menu-item-<?php echo $menu->ID;?>">
				<a href="<?php echo $menu->url;?>"><?php echo $menu->title;?></a>
				<?php if($menu->type=='taxonomy'){
				$terms = get_term_children($menu->object_id,"categories");
				if(!empty($terms)){
				?>
				<ul class="childs" >
					<?php wp_list_categories('hide_empty=0&show_option_none=&title_li=&taxonomy=categories&child_of='.$menu->object_id);?>
				</ul>
				<?php }
				} ?>
			</li>
			<?php
		}
	}?>
    </ul> <?php
}

?>
